<?php

class FuneralDirectors extends Zend_Db_Table_Abstract
{
	// hmm.  why not just point this at users? maybe no real need.
	protected $_name = 'funeral_directors';
	
    public function get_personal_info( $uid )
    {
        if(empty($uid)) 
		{
			$results['status'] = 'FAILED_BAD_PARAM';
			return $results;	
		} 

		$results = array();
		try {
	        $info = $this->fetchRow("user_id=$uid");
			$results['status'] = 'SUCCESS';
			$results['rows'] = array( $info->toArray() );
		}
		catch(Exception $e)
		{	
			$results['status'] = 'FAILURE';
			$results['rows'] = array();
		}
		
        return $results;
    }

	public function getFuneralDirectorsByBranchID( $branch_id )
	{
		$results = array();
		
        if(!isset($branch_id) && empty($branch_id)) 
		{
			$results['status'] = 'FAILED_BAD_PARAM';
			return $results;	
		} 
	
		try {
	        $rows = $this->fetchAll("branch_id='$branch_id'");
			$directors = array();
			foreach( $rows as $row )
			{
				$directors[] = $row->toArray();
			}
			$results['status'] = 'SUCCEEDED';
			$results['rows'] = $directors;
		}
		catch(Exception $e)
		{	
			$results['status'] = 'FAILURE';
			$results['rows'] = array();
		}

		return $results;	
	}
	
	public function getFuneralHomeDirectors( $VAS_fh_id )
	{
		$results = array();
		
        if(!isset($VAS_fh_id) && empty($VAS_fh_id)) 
		{
			$results['status'] = 'FAILED_BAD_PARAM';
			return $results;	
		} 
	
		try {
	        $rows = $this->fetchAll("funeral_home_id=$VAS_fh_id");
			$directors = array();
			foreach( $rows as $row )
				$directors[] = $row->toArray();
			$results['status'] = 'SUCCEEDED';
			$results['rows'] = $directors;
		}
		catch(Exception $e)
		{	
			$results['status'] = 'FAILURE';
			$results['rows'] = array();
		}

		return $results;	
	}
	
    public function getProfileForDirectorByVASUserId( $VAS_user_id )
    {
		$results = array();
		
        if(empty($VAS_user_id)) 
		{
			$results['status'] = 'FAILED_BAD_PARAM';
			return $results;	
		} 

		try {
	        $profile = $this->fetchRow("user_id=$VAS_user_id");
			$profile_row = $profile->toArray();
			$results['status'] = 'SUCCEEDED';
			$results['rows'] = array( $profile->toArray() );
		}
		catch(Exception $e)
		{	
			$results['status'] = 'FAILURE';
			$results['rows'] = array();
		}
		
        return $results;
    }
	
    public function getProfileForDirectorByVASId( $VAS_id )
    {
		$results = array();
		
        if(empty($VAS_id)) 
		{
			$results['status'] = 'FAILED_BAD_PARAM';
			return $results;	
		} 

		try {
	        $profile = $this->fetchRow("id=$VAS_id");
	        if(null != $profile)
	        {
				$results['status'] = 'SUCCESS';
				$results['rows'] = array( $profile->toArray() );
			}
			else
			{
				$results['status'] = 'FAILURE';
				$results['rows'] = array();
			}
		}
		catch(Exception $e)
		{	
			$results['status'] = 'FAILURE';
			$results['rows'] = array();
		}
		
        return $results;
    }

	public function getProfileForDirectorByFDMSId( $fdmsId )
	{
        if(empty($fdmsId)) 
		{
			$results['status'] = 'FAILED_BAD_PARAM';
			return $results;	
		} 

		$results = array();
		try {
	        $profile = $this->fetchRow("FDMS_dir_id=$fdmsId");
			$results['status'] = 'SUCCESS';
			$results['rows'] = array( $profile->toArray() );
		}
		catch(Exception $e)
		{	
			$results['status'] = 'FAILURE';
			$results['rows'] = array();
		}
		
        return $results;
	}
	
    public function setPictureForDirector( $picture_path, $VAS_id )
    {
        if(empty($VAS_id)) 
		{
			$results['status'] = 'FAILED_BAD_PARAM';
			return $results;	
		} 

        if(!isset($picture_path)) 
		{
			$results['status'] = 'FAILED_BAD_PARAM';
			return $results;	
		} 

        $fd_profile = array();
        $fd_profile['photo'] = $picture_path;
			
		$results = array();
		try {
			$this->update($fd_profile, "id=$VAS_id");		
			$results['status'] = 'SUCCEEDED';
			$results['affected_rows'] = 1;
			$results['rows'] = array();
		}
		catch(Exception $e)
		{
			$results['status'] = 'FAILED';
			$results['affected_rows'] = 0;
			$results['rows'] = array();
		}
		
        return $results;		
    }

	// these next methods are mainly used in syncing with FDMS	
    public function addFuneralDirector( $profile )
    {
        if(!isset($profile)) 
		{
			$results['status'] = 'FAILED_BAD_PARAM';
			return $results;	
		} 
		
        $fd_profile = array();

        if(isset($profile['fname']))
            $fd_profile['fname'] = $profile['fname'];
        if(isset($profile['mname']))
            $fd_profile['mname'] = $profile['mname'];
        if(isset($profile['lname']))
            $fd_profile['lname'] = $profile['lname'];
        if(isset($profile['phone']))
            $fd_profile['phone'] = $profile['phone'];
        if(isset($profile['address']))
            $fd_profile['address'] = $profile['address'];
        if(isset($profile['city']))
            $fd_profile['city'] = $profile['city'];
        if(isset($profile['state']))
            $fd_profile['state'] = $profile['state'];
        if(isset($profile['zip']))
            $fd_profile['zip'] = $profile['zip'];
        if(isset($profile['email']))
            $fd_profile['email'] = $profile['email'];
        if(isset($profile['FDMS_id']))
            $fd_profile['FDMS_id'] = $profile['FDMS_id'];
        
 		$results = array();
		try {
 	        return $this->insert($fd_profile);
 			$results['status'] = 'SUCCEEDED';
			$results['affected_rows'] = 1;
			$results['rows'] = array();
		}
		catch(Exception $e)
		{
			$results['status'] = 'FAILED';
			$results['affected_rows'] = 0;
			$results['rows'] = array();
		}
		
        return $results;
    }

    public function updateFuneralDirector( $profile, $VAS_id )
    {
        if(!isset($profile)) 
		{
			$results['status'] = 'FAILED_BAD_PARAM';
			return $results;	
		} 
		
        if(empty($VAS_id)) 
		{
			$results['status'] = 'FAILED_BAD_PARAM';
			return $results;	
		} 
		
        $fd_profile = array();

        if(isset($profile['fname']))
            $fd_profile['fname'] = $profile['fname'];
        if(isset($profile['mname']))
            $fd_profile['mname'] = $profile['mname'];
        if(isset($profile['lname']))
            $fd_profile['lname'] = $profile['lname'];
        if(isset($profile['phone']))
            $fd_profile['phone'] = $profile['phone'];
        if(isset($profile['address']))
            $fd_profile['address'] = $profile['address'];
        if(isset($profile['city']))
            $fd_profile['city'] = $profile['city'];
        if(isset($profile['state']))
            $fd_profile['state'] = $profile['state'];
        if(isset($profile['zip']))
            $fd_profile['zip'] = $profile['zip'];
        if(isset($profile['email']))
            $fd_profile['email'] = $profile['email'];
        if(isset($profile['FDMS_id']))
            $fd_profile['FDMS_id'] = $profile['FDMS_id'];
        
 		$results = array();
		try {
 	        return $this->update($fd_profile, "id=$VAS_id");
 			$results['status'] = 'SUCCEEDED';
			$results['affected_rows'] = 1;
			$results['rows'] = array();
		}
		catch(Exception $e)
		{
			$results['status'] = 'FAILED';
			$results['affected_rows'] = 0;
			$results['rows'] = array();
		}
		
        return $results;
    }
	
	public function syncWithFDMS( $funeral_home_id, $FDMS_funeral_home_id )
	{
	}
}

?>
